package com.leonardo.beanme.fields.abs;

import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;

import com.leonardo.beanme.fields.InputField;

public abstract class TextInput implements InputField {

	protected static final int FIELD_SIZE = 30;
	private TextField field;
	private boolean lock;
	
	protected TextInput(int constraint) {
		this.field = new TextField("", null, FIELD_SIZE, constraint);
	}
	
	public void setLabel(String label) {
		field.setLabel(label);
	}
	
	public void setValue(Object obj) {
		if(!lock && obj != null && !invalidValue(obj.toString()))
			field.setString(String.valueOf(obj));
		else
			field.setString(getDefaultValue().toString());
	}
	
	public Item getField() {
		return field;
	}
	
	public String getLabel() {
		return field.getLabel();
	}
	
	public void clear() {
		field.setString("");
	}
	
	protected boolean invalidValue(String value) {
		return value == null || value.equals("");
	}
	
	public void setName(String name) {}
	
	public void lock() {
		lock = true;
	}
	
	public void unlock() {
		lock = false;
	}
}
